package com.ml.carManager.net;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.hjq.toast.ToastUtils;
import com.ml.carManager.ui.dialog.SimpleHintDialog;
import com.ml.carManager.ui.dialog.listener.OnEnterClickListener;
import com.ml.carManager.ui.login.LoginActivity;
import com.ml.carManager.utils.auth.AuthManager;

import custom.base.data.Global;
import custom.base.entity.base.BaseResponse;
import custom.base.http.data.NetCode;
import custom.base.http.listener.AliveCallback;
import custom.base.http.listener.ResponseListener;

import custom.base.ui.activity.AppManager;
import retrofit2.Call;

import static custom.base.data.GlobalData.HINT_TOKEN_DIALOG_IS_SHOW;


/**
 * Created by cubebox on 2017/7/25.
 * <p>
 * 本类为net代理类，主要负责net的code错误时的全局处理
 */

public abstract class NetProxyListener<T> extends ResponseListener<T> implements NetCode {

    public NetProxyListener(AliveCallback liveNetCallback) {
        super(liveNetCallback);
    }

    public NetProxyListener(Context context) {
        super(context);
    }

    @Override
    public void onResponseCodeError(Call call, BaseResponse<T> response) {
        switch (response.getCode()) {
            case CODE_TOKEN_INAVALILABLE://token验证失败，重新登录
                if (context == null)
                    return;
                if (!Global.getInstance().getGlobalBooleanData(HINT_TOKEN_DIALOG_IS_SHOW)) {
                    AuthManager.getInstance(liveNetCallback.currentContext()).clearAuth(true);
                    SimpleHintDialog dialog = new SimpleHintDialog(liveNetCallback.currentContext());
                    dialog.setContentString("您的登录已经失效，请重新登录！谢谢。");
                    dialog.setCancelable(false);
                    dialog.setShowCancel(false);
                    dialog.setConfirmBtnText("我知道了");
                    dialog.setOnEnterClickListener(new OnEnterClickListener() {
                        @Override
                        public void onEnter() {
                            if (!liveNetCallback.isAlive())
                                return;
                            context.startActivity(new Intent(context, LoginActivity.class));
                            AppManager.getInstance().finishAllActivity();
                        }
                    });
                    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            Global.getInstance().putGlobalData(HINT_TOKEN_DIALOG_IS_SHOW, false);
                        }
                    });
                    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            Global.getInstance().putGlobalData(HINT_TOKEN_DIALOG_IS_SHOW, false);
                        }
                    });
                    dialog.show();
                    Global.getInstance().putGlobalData(HINT_TOKEN_DIALOG_IS_SHOW, true);
                }
                break;
            default:
                ToastUtils.show(response.getMsg());
                break;
        }

    }

    @Override
    public void onResponseError(Call call, Throwable t) {
        super.onResponseError(call, t);
        ToastUtils.show("当前网络不可用");
    }
}
